package com.ksm.client.document.presenter;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.ksm.client.ManagerEventBus;
import com.ksm.client.document.presenter.DocumentSearchPresenter.IDocumentSearchView;
import com.ksm.client.document.service.IKsmServiceAsync;
import com.ksm.client.document.view.DocumentSearchView;
import com.ksm.transfer.dto.DocumentDTO;
import com.ksm.transfer.dto.DocumentFilterDTO;
import com.ksm.transfer.dto.TypeDocumentDTO;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.mvp4g.client.view.ReverseViewInterface;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.TextField;

@Presenter(view = DocumentSearchView.class)
public class DocumentSearchPresenter extends BasePresenter<IDocumentSearchView, ManagerEventBus> implements
		IDocumentSearchPresenter {

	@Inject
	IKsmServiceAsync ksmService;

	public interface IDocumentSearchView extends IsWidget, ReverseViewInterface<IDocumentSearchPresenter> {

		public TextField getInterlocuteur();

		public void setInterlocuteur(TextField interlocuteur);

		public NumberField<Double> getMinAmount();

		public void setMinAmount(NumberField<Double> minAmount);

		public NumberField<Double> getMaxAmount();

		public void setMaxAmount(NumberField<Double> maxAmount);

		public DateField getBeginDate();

		public void setBeginDate(DateField beginDate);

		public DateField getEndDate();

		public void setEndDate(DateField endDate);

		public ComboBox<TypeDocumentDTO> getComboBoxType();

		public void setComboBoxType(ComboBox<TypeDocumentDTO> comboBoxType);

		public void initComboType(List<TypeDocumentDTO> elements);

	}

	public void onDocumentSaved() {

	}

	@Override
	public void findDocument() {
		DocumentFilterDTO filterDTO = prepareSearchDTO();
		ksmService.findDocumentByFilter(filterDTO, new AsyncCallback<List<DocumentDTO>>() {

			@Override
			public void onSuccess(List<DocumentDTO> documents) {
				System.out.println("success");
				getEventBus().documentGridUpdate(documents);
			}

			@Override
			public void onFailure(Throwable arg0) {
				System.out.println("error" + arg0.getStackTrace());
			}
		});

	}

	public DocumentFilterDTO prepareSearchDTO() {

		DocumentFilterDTO filterDTO = new DocumentFilterDTO();
		filterDTO.setType(getView().getComboBoxType().getValue().getName());
		filterDTO.setDateDebut(getView().getBeginDate().getCurrentValue());
		filterDTO.setDateFin(getView().getEndDate().getCurrentValue());
		filterDTO.setInterlocuteur(getView().getInterlocuteur().getCurrentValue());
		filterDTO.setMontantMin(getView().getMinAmount().getCurrentValue());
		filterDTO.setMontantMax(getView().getMaxAmount().getCurrentValue());

		return filterDTO;
	}

	public void onReferentialTypeDocumentUpdate(List<TypeDocumentDTO> typeDocumentDTOs) {
		getView().initComboType(typeDocumentDTOs);
	}

}
